﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace GenerateFileHash
{
    public class HashIt
    {
       
        private string filePath;
        private byte[] hash;
        private string hashAsString;

        public string HashAsString
        {
            get { return hashAsString; }
            set { hashAsString = value; }
        }
        
        public byte[] Hash
        {
            get { return hash; }
            set { hash = value; }
        }
        
	    public string FilePath
	    {
		    get { return filePath;}
		    set { filePath = value;}
	    }
	
       
        public HashIt(string filePath) : base()
        {
             SHA256 mySHA256 = SHA256Managed.Create();

             using (FileStream fileStream = File.OpenRead(filePath))
             {
                 // Be sure it's positioned to the beginning of the stream.
                 fileStream.Position = 0;
                 // Compute the hash of the fileStream.

                 Hash = mySHA256.ComputeHash(fileStream);
                 StringBuilder stringBuilder = new StringBuilder();

                 foreach (byte b in Hash)
                     stringBuilder.AppendFormat("{0:X2}", b);

                 HashAsString = stringBuilder.ToString();
                 // Close the file.
                 fileStream.Close();
             }   
        }

        public override string ToString()
        {
            return HashAsString; 
        }
    }

  
}
